<?php

	// Session
	session_start();
	
	// Nese vizitori eshte i identifikuar
	if (!isset($_SESSION['user_id'])) {
		header("Location: login.php");
	}
	
	// Integrimi i skedave sistemore
    include('../include/functions.inc.php');
	include('../include/template.class.php');
	
	// Project
	$PROJECT = setProject();
	
	
	// Ndyshoret primare
	define('TPL','../include/styles/'.$PROJECT['style'].'/members/content.html');
	define('ID',$_GET['id']);
	define('UID',$_SESSION['user_id']);
	define('FILENAME',basename($_SERVER["PHP_SELF"]));
	
	
	// Connection
	dbOpen("comunity");
	
	
	// Inicializimi i shablonave
	$tmp = new Template(TPL,$PROJECT['lang']);
	
	
	// INDEXING OPTIONS
	$INDEX['sid'] = indexing("SELECT id, name FROM sections");
	$INDEX['access'] = $tmp->indexing("access");
	
	
	// MENUES
	$tmp->setContent("CATEGORY",$tmp->menu("category","../members"));
	$tmp->setContent("MYMENU",$tmp->menu("mymenu","books.php"));
	$tmp->setContent("FOOTMENU",$tmp->menu("foot"));
	$tmp->setContent("MYBOOK",$tmp->menu("mybook",FILENAME,'?id='.ID));
	
	
	// Nese akoma forma nuk eshte aplikuar
	if (!isset($_POST['send'])) {
		
		// Book details
		$_POST = mysql_fetch_assoc(mysql_query("SELECT id,typ,sid,volume,intro,title,access FROM library WHERE id = '".ID."'"));
		
		// Content
		$_POST['content'] = implode(mysql_fetch_assoc(mysql_query("SELECT content FROM contents WHERE book_id = '".ID."'")));
	}
	
	
	// OPTIONS
	$OPTIONS['sid'] = selectOptions("SELECT id,name FROM sections",$_POST['sid'],"newsid");
	$OPTIONS['volume'] = selectOptions("SELECT volume FROM library WHERE volume != '' GROUP BY volume ORDER BY volume ASC",$_POST['volume'],"newvolume");
	$tmp->setOptions($_POST,$OPTIONS);
	
	
	// INFOBOOK
	$BOOK = infoBook(ID,$INDEX);
	$tmp->setContent("INFOBOOK",$BOOK);
	
	
	// HELP
	$tmp->setContent("EDITOR",$tmp->xmlContent("editor","help.xml"));
	
	
	// SUBMIT FORM
	if(isset($_POST['send'])) {
		
		// SID NULL
		if(empty($_POST['sid'])) 
			$ERROR['sid'] = "null";
		// NEW SID
		elseif($_POST['sid'] == "other") {	
			// NEWSID NULL
			if(empty($_POST['newsid'])) 
				$ERROR['newsid'] = "null";
		}
		
		// TITLE NULL
		if(empty($_POST['title'])) 
			$ERROR['title'] = "null";
			
		// NEWVOLUME
		if(($_POST['volume'] == "other")) {
			// NEWVOLUME NULL
			if(empty($_POST['newvolume']))
				$ERROR['newvolume'] = "null"; 
		}
		
		// CONTENT NULL
		if(empty($_POST['content']))
			$ERROR['content'] = "null";
			
		// VOLUME
		if(!empty($_POST['volume'])) {
			// Bashkangjit vleren e permledhjes se re ndyshores volume
			if(!empty($_POST['newvolume']))
				$_POST['volume'] = $_POST['newvolume'];
		}
		
			
		// EMPTY ERRORS
		if(empty($ERROR)) {
			
			// INSERT NEWSID
			if(!empty($_POST['newsid'])) {
				mysql_query("INSERT INTO sections SET access = '0', name = '".$_POST['newsid']."'");
				$_POST['sid'] = mysql_insert_id();
			}
			
			// UPDATE BOOK
			$update = mysql_query("UPDATE library SET
										typ = '".$_POST['typ']."',
										sid = '".$_POST['sid']."',
										volume = '".$_POST['volume']."',
										intro = '".$_POST['intro']."',
										title = '".$_POST['title']."',
										access = '".$_POST['access']."'
									WHERE id = '".ID."'
									");
			
			// Nese insertimi ne arkiven e titujve eshte i suksesshem
			if($update) {
				
				// Arkivo permbajtjen ne Content
				mysql_query("UPDATE contents SET 
								content = '".$_POST['content']."'
							 WHERE book_id = '".ID."'
							");
				
				// Kalo ne faqen e librave dhe trego porosin per arkivim te sukseshem
				header('Location: books.php?splash=editbook');
				exit;
			}
			else {
				// Nese per moment arkivimi eshte i pamundshem
				header('Location: books.php?splash=busy');
				exit;
			}
		}
	}
	
	// LABELS
	$tmp->setLabels();
	
	
	// Convert POST and ERROR vars in Form
	$tmp->setForm($_POST,$ERROR);
	
	
	// BOOK
	$tmp->setContent($BOOK);
	
	
	// Dhe cdo gje ka nje fund, trego faqen						  
	echo $tmp->vorlage;

?>